define([], function () {
    //加载评论
    window.loadcomment = function (page) {
        var isReply = $(".comment-panel .commentpost").length > 0 ? true : false;
        if (isReply) {
            $(".commentpost h3 > a").trigger("click");
            page = parseInt($(".commentlist .pager li.active").text());
        }
        Fast.api.ajax({
            url: "/comment/load",
            data: {type: $(".commentpost input[name=type]").val(), name: $(".commentpost input[name=name]").val(), page: page},
        }, function (data, ret) {
            $(".commentlist").html(data);
            if (!isReply) {
                $('html, body').animate({scrollTop: $('.nav-noborder').offset().top - 60}, 'slow');
            }
            return false;
        });
    };
    //发表评论
    $(".commentpost form").on("submit", function (e) {
        e.preventDefault();
        var form = this;
        var btn = $("button[type=submit]", this);
        var content = $("textarea", this).val();
        var pid = parseInt($("input[name=pid]", this).val());
        var score = parseInt($("input[name=score]", this).val());
        if (content == '') {
            Layer.msg("评论内容不能为空!");
            return false;
        }
        if ($("input[name=score]", this).length > 0 && pid == 0 && score == 0) {
            Layer.msg("插件评分不能为空!");
            return false;
        }
        btn.attr("disabled", "disabled");
        Fast.api.ajax({
            url: $(this).attr("action"),
            data: $(this).serialize(),
            complete: function (xhr) {
                var token = xhr.getResponseHeader('__token__');
                if (token) {
                    $("input[name='__token__']", form).val(token);
                }
            }
        }, function (data, ret) {
            btn.removeAttr("disabled");
            $("#pid").val(0);
            $("input[name=score]", form).val(0);
            $("#commentcontent").val('');
            $("#commentcount").text(parseInt($("#commentcount").text()) + 1);
            $(".commentpost .rating .rating-star").removeClass("checked");
            loadcomment(1);
        }, function (data, ret) {
            btn.removeAttr("disabled");
            if (ret.msg.indexOf("请登录后再操作") > -1) {
                Fast.api.account(function () {
                    $(form).trigger("submit");
                });
                return false;
            }
        });
        return false;
    });
    //Ctrl+回车提交
    $(document).on("keydown", "#commentcontent", function (e) {
        if (e.ctrlKey && e.which == 13) {
            $(".commentpost form").trigger("submit");
        }
    });
    //点击回复
    $(document).on("click", ".comment-reply > a", function () {
        $("#pid").val($(this).data("id"));
        $(this).parent().parent().append($(".commentpost").detach());
        $(".commentpost h3 > a").show();
        $(".commentpost .rating").hide();
        $("#commentcontent").focus();
    });
    //点击取消回复
    $(document).on("click", ".commentpost h3 > a", function () {
        $(".commentpost .rating").show();
        $(".commentpost h3 > a").hide();
        $(".commentlist").after($(".commentpost").detach());
    });
    //点击评分
    $(document).on("click", ".rating.vote > .rating-star", function () {
        $(".rating.vote > .rating-star").removeClass("checked");
        $(this).addClass("checked");
        $("input[name=score]", $(this).closest("form")).val($(this).data("value"));
    });
});
